package com.smartens.boit.util;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;

public class EditTextUtil {

    private final Context context;
    private final View view;
    private KeyboardLayoutListener mListener;
    private boolean mIsKeyboardActive = false; //&emsp;输入法是否激活
    private int mKeyboardHeight = 0; // 输入法高度
    private static volatile EditTextUtil editTextUtil = null;
    private int lastkeyboardHeight=-1;

    private EditTextUtil(View view, Context context) {
        this.view = view;
        this.context = context;
    }

    public static  EditTextUtil getInstance(View view, Context context) {
        if (editTextUtil == null) {
            synchronized (EditTextUtil.class) {
                if (editTextUtil == null) {
                    editTextUtil = new EditTextUtil(view, context);

                }
            }
        }
        return editTextUtil;
    }

    public static EditTextUtil getInstance() throws Exception {
        if (editTextUtil == null) {
            synchronized (EditTextUtil.class) {
                if (editTextUtil == null) {
                    throw new Exception("nullEditTextUtil");
                }
            }
        }
        return editTextUtil;
    }

    private class KeyboardOnGlobalChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private  int mScreenHeight=0;
        Rect mRect = new Rect();
        private int getScreenHeight() {
            if (mScreenHeight > 0) {
                return mScreenHeight;
            }
            mScreenHeight = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay().getHeight();
            return mScreenHeight;
        }

        @Override
        public void onGlobalLayout() {
            // 获取当前页面窗口的显示范围
            view.getWindowVisibleDisplayFrame(mRect);
            int screenHeight = getScreenHeight(); //屏幕高度
            int keyboardHeight = screenHeight - mRect.bottom; // 输入法的高度
            if (lastkeyboardHeight!=-1)
            {
                if (lastkeyboardHeight==keyboardHeight)
                {
                    return;
                }
                if (lastkeyboardHeight>keyboardHeight)
                {
                    mIsKeyboardActive=false;
                }

                else if (lastkeyboardHeight<keyboardHeight)
                {
                    mIsKeyboardActive = true;
                    mKeyboardHeight = keyboardHeight;
                }
                if (mListener != null) {
                    mListener.onKeyboardStateChanged(mIsKeyboardActive, keyboardHeight);
                }
            }
            lastkeyboardHeight=keyboardHeight;
        /*    if (Math.abs(keyboardHeight) > screenHeight / 5) {
                isActive = true; // 超过屏幕五分之一则表示弹出了输入法
                mKeyboardHeight = keyboardHeight;
            }*/

        }
    }

    public void setKeyboardListener(KeyboardLayoutListener listener) {
        mListener = listener;
        ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());
    }

    public KeyboardLayoutListener getKeyboardListener() {
        return mListener;
    }

    public boolean isKeyboardActive() {
        return mIsKeyboardActive;
    }

    /**
     * 获取输入法高度
     *
     * @return
     */
    public int getKeyboardHeight() {
        return mKeyboardHeight;
    }

    public interface KeyboardLayoutListener {
        /**
         * @param isActive       输入法是否激活
         * @param keyboardHeight 输入法面板高度
         */
        void onKeyboardStateChanged(boolean isActive, int keyboardHeight);
    }


}
